package client;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;

import javax.swing.*;

public class ListaDinosauri extends JFrame implements ActionListener,Serializable {

	private JButton[] dinosauri;
	
	private int risp ;
	private int[] lista1;
	
	private static final int  X = 200;
	private static final int  Y = 30;
	
	public ListaDinosauri(int[] lista){
		
		super("Lista Dinosauri");
		setLayout(new FlowLayout());
		
		lista1 = new int[lista.length];
		lista1 = lista;
		
		setSize(210,(30*lista.length)+10);
		
		JPanel pannello = new JPanel();
		
		pannello.setLayout(new GridLayout(lista.length,1));
		dinosauri = new JButton[lista.length];
		for(int i=0;i<lista.length;i++){
			dinosauri[i]=new JButton("dinosauro "+lista[i]);
			dinosauri[i].setPreferredSize(new Dimension(X, Y));
			dinosauri[i].addActionListener(this);
			pannello.add(dinosauri[i]);
		}
		
		add(pannello);
		pack();
		validate();
		setVisible(true);	
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		synchronized(this){
			for(int i=0;i<lista1.length;i++){
				if(e.getSource().equals(dinosauri[i])){
				risp=lista1[i];
				notifyAll();				
				}
			}
		}
	}
	
	public int getRisp(){
		synchronized(this){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		dispose();
		return risp;
	}
	
	
}